# 爬取百度图片的街拍信息
import os, time
import requests
import json
from urllib.parse import urlencode
from urllib import request

# from urllib.request import urlretrieve

# # 获取可以维持会话状态的请求对象
# m = requests.Session()

headers = {
    'Accept': 'text/plain, */*;q=0.01',
    'Accept-Encoding': 'deflate',
    'Accept-Language': 'zh-CN, zh;q=0.9, zh-TW;q=0.8, fr;q=0.7, en;q=0.6',
    'Connection': 'keep-alive',
    'Host': 'image.baidu.com',
    'Referer': 'https://image.baidu.com',
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) Ap'
                  'pleWebKit/537.36 (KHTML, like Gecko) Chrome'
                  '/64.0.3282.119 Safari/537.36',
    'X-Requested-With': 'XMLHttpRequest',
}


def getPage(offset, keyword):
    '''爬取信息'''
    # 定义参数
    params = {
        'tn': 'resultjson_com',
        'ipn': 'rj',
        'ct': '201326592',
        'is': '',
        'fp': 'result',
        'queryWord': keyword,
        'cl': '2',
        'lm': '-1',
        'ie': 'utf-8',
        'oe': 'utf-8',
        'adpicid': '',
        'st': '',
        'z': '',
        'ic': '',
        'word': keyword,
        's': '',
        'se': '',
        'tab': '',
        'width': '',
        'height': '',
        'face': '',
        'istype': '',
        'qc': '',
        'nc': '1',
        'fr': '',
        'pn': str(offset),
        'rn': '30',
        'gsm': '78',
        '1534994985052': '',
    }

    # 初始化url地址
    url = 'http://image.baidu.com/search/acjson?' + urlencode(params)
    try:
        res = requests.get(url, headers=headers)
        if res.status_code == 200:
            return res.content.decode('utf-8')
    except:
        # print(1)
        return None


def getImage(content):
    '''获取图片信息'''
    data = json.loads(content)
    data = data['data']
    if data:
        for item in data:
            image = item.get("middleURL", [])
            title = item.get("fromPageTitleEnc")
            pictype = item.get("type")
            yield {
                'image': image,
                'title': title,
                'pagenum': item.get('pageNum'),
                'type': pictype,
            }


def saveImage(item):
    '''储存图片信息'''
    # global m
    # 图片路径的处理
    image_url = item.get("image")

    try:
        s = item.get('title').replace(':', ' ')
        save_pic = './baidupic/' + str(
                item.get('pagenum')) + ' ' + s + '.' + item.get('type')
    except:
        save_pic = None

    # 存储图片
    try:
        # 防盗链下载图片方法
        opener = request.build_opener()
        opener.addheaders = [('User-Agent',
                              'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKi'
                              't/537.36 (KHTML, like Gecko) Chrome/36.0.1941.0'
                              ' Safari/537.36'),
                             ('Referer', 'https://image.baidu.com')]
        request.install_opener(opener)
        request.urlretrieve(image_url, save_pic)
    except Exception as e:
        print('图片保存失败！', e)


def main(offset, qword='街拍'):
    '''主函数'''
    content = getPage(offset, qword)
    for item in getImage(content):
        print(item)
        saveImage(item)


if __name__ == "__main__":
    # s = input('请输入搜索关键字，直接回车搜索"街拍":\n')
    # if s == '':
    #     s = '街拍'
    if not os.path.exists('./baidupic'):
        os.mkdir('./baidupic')
    for i in range(2):
        main(offset=i * 30)
        time.sleep(1)
